package com.hzhc.controller.baseinfo;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrInfoService;
import com.hzhc.service.SysUserService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CntrInfoVo;
import com.hzhc.vo.CntrStatisticsVo;
import com.hzhc.vo.LockVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 集装箱信息 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/cntrInfo")
@RequiredArgsConstructor
public class CntrInfoController extends BaseController {

    private final CntrInfoService infoService;

    private final SysUserService userService;

    /**
     * 集装箱列表 list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getAllList")
    @ResponseBody
    public ResultUtil<List<CntrInfoVo>> getAllList(HttpServletRequest request, @RequestBody CntrInfoVo vo){
        Page<CntrInfoVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<CntrInfoVo> list = infoService.getAllList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 add
     * @param request
     * @param info
     * @return
     */
    @PostMapping("/addCntrInfo")
    @SysTemLog(operModul = "箱管理-集装箱管理",operType = "ADD",operDesc = "集装箱管理添加",modular="PC")
    public ResultUtil<T> addCntrInfo(HttpServletRequest request,@RequestBody CntrInfo info){
        Long userID = TokenUtil.getUserID(request);
        info.setUserId(userID);
        info.setUserName(userService.getById(userID).getRealname());
        info.setAccesspath("PC端");
        return infoService.addCntrInfo(info);
    }

    /**
     * 箱号 check
     * @param request
     * @param info
     * @return
     */
    @PostMapping("/checkCntrNo")
    public ResultUtil<T> checkCntrNo(HttpServletRequest request,@RequestBody CntrInfo info){
        Boolean b = infoService.checkCntrNo(info);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.build(ResultUtilCodeEnum.CNTR_NO_EXIST);
    }

    /**
     * 贝位占用 check
     * @param request
     * @param info
     * @return
     */
    @PostMapping("/checkBay")
    public ResultUtil<T> checkBay(HttpServletRequest request,@RequestBody CntrInfo info){
        return infoService.checkBay(info);
    }

    /**
     * 集装箱修改 update
     * @param request
     * @param info
     * @return
     */
    @PostMapping("/updateCntrInfo")
    @SysTemLog(operModul = "箱管理-集装箱管理",operType = "UPDATE",operDesc = "集装箱管理修改",modular="PC")
    public ResultUtil<T> updateCntrInfo(HttpServletRequest request,@RequestBody CntrInfo info){
        info.setAccesspath("PC端");
        return infoService.updateCntrInfo(info);
    }



    /**
     * 集装箱删除 del
     * @param request
     * @param cntrNo
     * @return
     */
    @GetMapping("/delCntrInfo/{cntrNo}")
    @SysTemLog(operModul = "箱管理-集装箱管理",operType = "DELETE",operDesc = "集装箱管理删除",modular="PC")
    public ResultUtil<T> delCntrInfo(HttpServletRequest request,@PathVariable("cntrNo") String cntrNo){
        return infoService.delCntrInfo(cntrNo);
    }

    /**
     * 在场箱货主统计
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getComCntrList")
    public ResultUtil<List<CntrStatisticsVo>> getComCntrList(HttpServletRequest request,@RequestBody CntrStatisticsVo vo){
        List<CntrStatisticsVo> list = infoService.getComCntrList(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 在场箱货主统计--导出(20240613暂不用)
     * @param request
     * @param response
     * @param vo
     */
    @GetMapping("/exportComCntr")
    @SysTemLog(operModul = "在场箱货主统计导出", operType = "导出", operDesc = "在场箱货主统计导出功能", modular="PC")
    public void export(HttpServletRequest request, HttpServletResponse response, CntrStatisticsVo vo) {
        infoService.exportComCntr(request, response, vo);
    }


    /**
     * 锁箱
     * @param request
     * @return
     */
    @RequestMapping("/lockCntr")
    public ResultUtil<T> lockCntr(HttpServletRequest request,@RequestBody LockVo vo){
        List<CntrInfo> cntrList = vo.getCntrList();
        return infoService.lockCntr(cntrList,vo.getLockRemark());
    }

    /**
     * 解锁箱
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/unLockCntr")
    public ResultUtil<T> unLockCntr(HttpServletRequest request,@RequestBody LockVo vo){
        List<CntrInfo> cntrList = vo.getCntrList();
        return infoService.unLockCntr(cntrList);
    }
    /**
     * 阳逻港铁水联运电子库场图各区箱子计数
     */
    @RequestMapping("/getYardAreaCount")
    public ResultUtil<JSONObject> getYardAreaCount() {
        CntrInfo cntrInfo = new CntrInfo();
        JSONObject list = infoService.getCntrInfo(cntrInfo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    private final static String EXPORT_EXCEL_PATH = "static/download/发运模板.xlsx";
    private final static String EXPORT_EXCEL_PATH1 = "static/download/到达模板.xlsx";
    private final static String EXPORT_EXCEL_PATH2 = "static/download/车皮导入模板.xlsx";

    /**
     * 清单导出模板下载
     */
    @GetMapping("/exportFile/{flag}")
    public void export(@PathVariable("flag")Integer flag,HttpServletRequest request, HttpServletResponse response) {
        infoService.exportFile(request, response, EXPORT_EXCEL_PATH,EXPORT_EXCEL_PATH1,flag);
    }
    /**
     * 导入清单
     */
    @RequestMapping("/cntrUpload/{flag}")
    public ResultUtil<List<CntrInfoVo>> cntrUpload(@PathVariable("flag")Integer flag, MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return infoService.cntrUpload(flag,user,file);
    }

    /**
     * 车皮清单导出模板下载
     */
    @GetMapping("/exportChepiFile")
    public void exportChepiFile(HttpServletRequest request, HttpServletResponse response) {
        infoService.exportChePiFile(request, response, EXPORT_EXCEL_PATH2);
    }

    /**
     *车皮导入
     */
    @RequestMapping("/chepiUpload")
    public ResultUtil<List<CntrInfoVo>> chepiUpload(MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return infoService.chepiUpload(user,file);
    }
    /**
     * 保存上传的箱子
     */
    @PostMapping("/importCntrList")
    public ResultUtil<T> importCntrList(HttpServletRequest request, @RequestBody LockVo vo) {
        return infoService.importCntrList(request, vo.getCntrList());
    }

    /**
     * 回重保存上传的箱子车皮
     */
    @PostMapping("/importSgjCntrList")
    public ResultUtil<T> importSgjCntrList(HttpServletRequest request, @RequestBody CntrInfoVo vo) {
        return infoService.importSgjCntrList(request, vo.getCntrList());
    }
}
